#!/bin/bash

# 构建和发布 Franka Servo 包的脚本

set -e  # 遇到错误时退出

echo "🚀 开始构建 Franka Servo 包..."

# 清理之前的构建
echo "清理之前的构建文件..."
rm -rf build/
rm -rf dist/
rm -rf *.egg-info/

# 升级构建工具
echo "升级构建工具..."
pip install --upgrade pip setuptools wheel twine

# 构建包
echo "构建源码包和wheel包..."
python setup.py sdist bdist_wheel

# 检查构建的包
echo "检查构建的包..."
twine check dist/*

echo "✅ 包构建完成！"
echo ""
echo "构建的文件:"
ls -la dist/
echo ""

# 询问是否要安装到本地进行测试
read -p "是否要在本地安装进行测试？(y/n): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "在本地安装包进行测试..."
    pip install dist/*.whl --force-reinstall
    echo "本地安装完成！"
    
    # 测试导入
    echo "测试导入..."
    python -c "import frankaservo; print(f'✅ 导入成功，版本: {frankaservo.__version__}')"
fi

echo ""
echo "📦 构建完成！"
echo ""
echo "下一步操作:"
echo "1. 测试包: pip install dist/*.whl"
echo "2. 上传到测试 PyPI: twine upload --repository-url https://test.pypi.org/legacy/ dist/*"
echo "3. 上传到正式 PyPI: twine upload dist/*"
echo ""
echo "注意: 上传到 PyPI 需要先在 https://pypi.org 和 https://test.pypi.org 注册账户"
